package com.atguigu.juc.study.chapter09.atomics;

import java.util.concurrent.CountDownLatch;

/**
 * @author : leimingchao
 * @date Date : 2022年06月16日 0:34
 */
public class AccumulatorCompareDemo {

    public static final int _1W = 10000;

    public static final int threadNumber = 50;

    public static void main(String[] args) {

        ClickNumber clickNumber = new ClickNumber();

        long startTime;

        long endTime;

        CountDownLatch countDownLatch1 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch2 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch3 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch4 = new CountDownLatch(threadNumber);

        startTime = System.currentTimeMillis();

        for (int i = 1; i <= threadNumber; i++) {

            new Thread(() -> {
                try {
                    for (int j = 1; j <= _1W * 100; j++) {
                        clickNumber.clickBySynchronized();
                    }
                } finally {
                    countDownLatch1.countDown();
                }
            }, String.valueOf(i)).start();
        }

        try {
            countDownLatch1.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        endTime = System.currentTimeMillis();

        System.out.println("---costTime:" + (endTime - startTime) + " 毫秒" + "\t clickBySynchronized:" + clickNumber.getNumber());


        startTime = System.currentTimeMillis();

        for (int i = 1; i <= threadNumber; i++) {

            new Thread(() -> {
                try {
                    for (int j = 1; j <= _1W * 100; j++) {
                        clickNumber.clickByAtomicLong();
                    }
                } finally {
                    countDownLatch2.countDown();
                }
            }, String.valueOf(i)).start();
        }

        try {
            countDownLatch2.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        endTime = System.currentTimeMillis();

        System.out.println("---costTime:" + (endTime - startTime) + " 毫秒" + "\t clickByAtomicLong:" + clickNumber.getAtomicLong());

        startTime = System.currentTimeMillis();

        for (int i = 1; i <= threadNumber; i++) {

            new Thread(() -> {
                try {
                    for (int j = 1; j <= _1W * 100; j++) {
                        clickNumber.clickByLongAdder();
                    }
                } finally {
                    countDownLatch3.countDown();
                }
            }, String.valueOf(i)).start();
        }

        try {
            countDownLatch3.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        endTime = System.currentTimeMillis();

        System.out.println("---costTime:" + (endTime - startTime) + " 毫秒" + "\t clickByLongAdder:" + clickNumber.getLongAdder());

        startTime = System.currentTimeMillis();

        for (int i = 1; i <= threadNumber; i++) {

            new Thread(() -> {
                try {
                    for (int j = 1; j <= _1W * 100; j++) {
                        clickNumber.clickByLongAccumulator();
                    }
                } finally {
                    countDownLatch4.countDown();
                }
            }, String.valueOf(i)).start();
        }

        try {
            countDownLatch4.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        endTime = System.currentTimeMillis();

        System.out.println("---costTime:" + (endTime - startTime) + " 毫秒" + "\t clickByLongAccumulator:" + clickNumber.getLongAccumulator());
    }
}
